import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { _HttpClient, SettingsService } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Api } from 'src/app/shared/api/api';
import { ResponseCode } from 'src/app/shared/response.code';

@Component({
  selector: 'app-account-settings-base',
  templateUrl: './base.component.html',
  styleUrls: ['./base.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ProfileBaseComponent implements OnInit {
  constructor(
    private settings: SettingsService,
    private http: _HttpClient,
    private msg: NzMessageService,
    public changeDetectorRef: ChangeDetectorRef
  ) {}
  avatar = '';
  userLoading = true;
  user: any;

  ngOnInit(): void {
    this.userLoading = false;
    this.user = this.settings.user;
  }

  save() {
    this.http.post(`${Api.BaseUserUpdateApi}`, this.user).subscribe((res: any) => {
      if (res) {
        if (res.code === ResponseCode.SUCCESS) {
          this.msg.success('保存成功');
          this.user.email = this.user.phone;
          this.settings.setUser(this.user);
        } else {
          this.msg.warning(res.message);
        }
      } else {
        this.msg.error('保存失败，未知错误');
      }
    });
  }
  change($event: any) {
    console.log($event.file.response.data)
    this.user.avatar = $event.file.response.data
    this.save()
  }
}
